Skip to content

fix(jsonl): make write_private_atomic work on Windows - #8

Open
enricodetoma wants to merge 2 commits into
xhluca:mainfrom
enricodetoma:main
Open

enricodetoma wants to merge 2 commits into
xhluca:mainfrom
enricodetoma:main

Conversation

@enricodetoma

Copy link
Copy Markdown

On Windows every write failed. os.fchmod() is Unix-only, so on Python <= 3.12 it raised AttributeError just after mkstemp() left the descriptor open; the cleanup unlink() then hit WinError 32 ("the file is being used by another process") and that secondary error replaced the real one, so users saw "cannot write target session ...: <winerror 32>" pointing at a target file that never existed. On Python 3.13, where os.fchmod exists, writes instead died in _fsync_directory(): Windows cannot os.open() a directory.

  • skip os.fchmod() where it is unavailable; Windows keeps the creating user's ACLs
  • skip the directory fsync on Windows; NTFS journaling keeps directory entries durable without it
  • make the temporary-file unlink in the error path best-effort so a cleanup failure cannot mask the original error being reported

Add regression tests for both failure modes. Verified on Windows with Python 3.11 and 3.13: a 371-record Codex-to-Claude transfer now completes, and write, collision refusal, and rollback behave as on POSIX.

Note: some existing tests remain POSIX-specific (permission-bit assertions, pty imports, CRLF-sensitive fixture bytes) and cannot pass on Windows regardless of this change; the Ubuntu CI matrix is unaffected.

On Windows every write failed. os.fchmod() is Unix-only, so on Python
<= 3.12 it raised AttributeError just after mkstemp() left the
descriptor open; the cleanup unlink() then hit WinError 32 ("the file
is being used by another process") and that secondary error replaced
the real one, so users saw "cannot write target session ...: <winerror
32>" pointing at a target file that never existed. On Python 3.13,
where os.fchmod exists, writes instead died in _fsync_directory():
Windows cannot os.open() a directory.

- skip os.fchmod() where it is unavailable; Windows keeps the creating
  user's ACLs
- skip the directory fsync on Windows; NTFS journaling keeps directory
  entries durable without it
- make the temporary-file unlink in the error path best-effort so a
  cleanup failure cannot mask the original error being reported

Add regression tests for both failure modes. Verified on Windows with
Python 3.11 and 3.13: a 371-record Codex-to-Claude transfer now
completes, and write, collision refusal, and rollback behave as on
POSIX.

Note: some existing tests remain POSIX-specific (permission-bit
assertions, pty imports, CRLF-sensitive fixture bytes) and cannot pass
on Windows regardless of this change; the Ubuntu CI matrix is
unaffected.

@xhluca xhluca left a comment •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed head 8ee41da7433374803373ba7f66d6740a89757b0f. This is a focused and useful portability fix. The isolated Linux Python 3.11 suite passes (1,521 passed, 55 skipped), including the new unavailable-fchmod and cleanup-error tests. I have not run a Windows host and do not count the contributor's Windows runtime report as my own validation.

Two narrow changes are needed before merge:

  1. [P2] Correct the privacy/durability guarantees (jsonl.py, lines 164 and 213–214; CHANGELOG.md). Skipping Unix permission enforcement does not guarantee an owner-only Windows ACL: default file ACLs inherit from the parent directory (Microsoft file security documentation). Likewise, NTFS journaling is not proof that a newly published directory entry is durable after a crash; Windows caches filesystem metadata (Microsoft file caching documentation). State the actual boundary: file data is flushed, directory crash durability remains platform-dependent, and Windows confidentiality depends on the destination ACL unless explicit enforcement is added. Do not claim POSIX mode-0600 equivalence.
  2. [P2] Cover the new Windows fsync branch directly. The two added tests both execute the POSIX directory-flush path. Add a test that selects the nt branch and asserts directory os.open/os.fsync are not called, alongside the existing POSIX durability tests. This can be mechanical and credential-free; an entire Windows CI conversion matrix is not required for this small PR.

A related pre-existing cleanup issue remains: if fchmod or fdopen raises before stream ownership is established, the raw descriptor stays open. The new suppression avoids masking the original error but does not close that descriptor. A focused follow-up should close owned descriptors before unlinking and test both setup failures; PR #10 contains an approach to this. I am distinguishing that inherited bug from the changes introduced here.

— Codex (OpenAI coding agent, GPT-6.1-sol)

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown

🟢 Contributor eligibility requirements met for @enricodetoma.

  • 🟢 Account created: 2010-11-02; age: 5820 days (required: at least six calendar months).
  • 🟢 Contributions dated on or before 2026-09-09: 107 or more (required: at least 100).

Pull request authors must have a GitHub account at least six calendar months old and at least 100 GitHub contributions dated on or before the date one calendar month before the check.

See the contributor eligibility policy.

…nc branch

Correct the docstring, comments, and changelog: skipping os.fchmod does not
make the file owner-only on Windows (it inherits the destination directory
ACL), and directory-entry crash durability is left to the platform rather
than guaranteed by NTFS journaling.

Add a test that selects the nt branch and asserts directory os.open and
os.fsync are not called.
@enricodetoma
enricodetoma requested a review from xhluca October 9, 2026 17:13
tudstudent added a commit to tudstudent/session-migrate that referenced this pull request Oct 12, 2026
…tests

Follow up on xhluca's review of PR xhluca#8: Windows confidentiality depends on destination ACLs rather than POSIX-mode equivalence, and directory crash durability remains platform-dependent.

Assert that the Windows directory-flush branch calls neither open nor fsync. Cover owned descriptor closure for both fchmod and fdopen setup failures. Preserve Enrico's original patch authorship and existing native manifest guard behavior.

Validation: Linux Python 3.12.3; Windows/jsonl/conversion/target integration gate 116 passed. Ruff, formatting, contributor-policy tests and whitespace checks pass. These mechanical Windows branches do not establish Windows-host native acceptance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants